package com.choosefine.it.worker;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import java.math.BigDecimal;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.team.dao.TeamPublishFindWorkerInfoMapper;
import com.choosefine.it.team.model.TeamPublishFindWorkerInfo;
import com.choosefine.it.team.service.TeamPublishFindWorkerInfoService;
import com.choosefine.it.worker.dao.MyWorksManagerMapper;
import com.choosefine.it.worker.dao.WorkerCenterMapper;
import com.choosefine.it.worker.model.FindWork;
import com.choosefine.it.worker.model.WorkDetailInfo;
import com.choosefine.it.worker.service.Impl.MyWorksManagerServiceImpl;
import com.choosefine.it.worker.service.Impl.WorkerCenterServiceImpl;

/**
 * 工人角色-我的中心业务-单元用力测试-Service服务层测试
 * 
 * 主要作用在于  - 测试推动开发，并测试业务逻辑
 * 
 * @注： 如果有直接请求别人的URL，请先放下，或你们可以依赖我的实现下方式
 * 
 * @author liaozhanggen 2016年12月26日
 */
@RunWith(SpringJUnit4ClassRunner.class)
public class WorkerCenterServiceTestCase {

	@InjectMocks
	private WorkerCenterServiceImpl workerCenterService;
	@InjectMocks
	private MyWorksManagerServiceImpl myWorksManagerServiceImpl;
	@Mock
	TeamPublishFindWorkerInfoMapper teamPublishFindWorkerInfoMapper;
	@Mock
	TeamPublishFindWorkerInfoService teamPublishFindWorkerInfoService;
	@Mock
	MyWorksManagerMapper myWorksManagerMapper;
	@Mock
	WorkerCenterMapper workerCenterMapper;
	@Mock
	MongoTemplate mongoTemplate;

	@Before
	public void setUp(){
		//初始化注入
		MockitoAnnotations.initMocks(this);
	}
	
	
	@Test
	public void should_worker_publish_worksuccess() throws BusinessException{
		//初始化数据
		FindWork findwork = initFindWork();
		//Mock方法，摸拟一个真实的对象，写给预期的一个返回值
		when(workerCenterMapper.insertPublishWork(Mockito.any(FindWork.class))).thenReturn(1);
		//Mock掉依赖调用的方法
		Mockito.doNothing().when(mongoTemplate).insert(Mockito.any(FindWork.class));
		//主方法调用
		int count = workerCenterService.insertPublishWork(findwork);
		
		//单元测试必须有的一个元素，断言，结果是不是正确
		assertEquals(count, 1);
	}
	
	@Test
	public void should_worker_publish_workerror() throws BusinessException{
		//初始化数据
		FindWork findwork = initFindWork();
		//Mock方法，摸拟一个真实的对象，写给预期的一个返回值
		when(workerCenterMapper.insertPublishWork(Mockito.any(FindWork.class))).thenReturn(0);
		//Mock掉依赖调用的方法
		Mockito.doNothing().when(mongoTemplate).insert(Mockito.any(FindWork.class));
		//主方法调用
		int count = workerCenterService.insertPublishWork(findwork);
		
		//单元测试必须有的一个元素，断言，结果是不是正确
		assertEquals(count, 0);
	}
	/** 
	 * @Description：工人首页-找工作-找工作-信息一览表-工作详情信息
	 * @Author：yuxin
	 * @Create Date：2017/2/23/0023
	 * @param：
	 * @return：
	 */
	@Test
	public void should_findTeamRecruitmentInfoById_success()throws Exception{
		TeamPublishFindWorkerInfo info2=new TeamPublishFindWorkerInfo();
		when(myWorksManagerMapper.findWorkerFavoriteTeamRecruitmentByUCode(Mockito.anyMap())).thenReturn(0);
		when(teamPublishFindWorkerInfoService.singleRecruitmentInfoById(Mockito.anyInt())).thenReturn(info2);
		/*TeamPublishFindWorkerInfo info1=myWorksManagerServiceImpl.findTeamRecruitmentInfoById(12,"asdasd");*/
		/*assertEquals(info2,info1);*/
	}
	@Test
	public void should_queryWorkerWorkDetialInfo_success()throws Exception{
		WorkDetailInfo Info=new WorkDetailInfo();
		Info.setTotalReceivable(new BigDecimal(11));
		when(myWorksManagerMapper.queryWorkerWorkDetialInfo(Mockito.anyString(),Mockito.anyInt())).thenReturn(Info);
		WorkDetailInfo Info1=myWorksManagerServiceImpl.queryWorkerWorkDetialInfo("asd",122);
		assertEquals(Info,Info1);
	}
	

	/**
	 * 初始化数据
	 * 
	 * @author liaozhanggen 2016年12月26日
	 */
	private FindWork initFindWork() {
		FindWork findWorkDto = new FindWork();
	/*	findWorkDto.setAreaCode("10000,10001|10000,20002");
		findWorkDto.setAreaName("金华|杭州");*/
		findWorkDto.setExpectSalary(new BigDecimal(200));
		findWorkDto.setFirstPhone("13692251785");
		findWorkDto.setUserCode("abc1000");
		findWorkDto.setUserName("张三");
		findWorkDto.setWorkTypeId("3");
		findWorkDto.setSettleType("1");
		return findWorkDto;
	}
}